﻿using System;
using System.Collections.Generic;

namespace Asteroids.Entities
{
    /// <summary>
    /// Encapsulates a group of <see cref="Xenon.Game.Entities.EntityProcessor"/>s
    /// </summary>
    public class EntityProcessorGroup
    {
        private string _name;
        private IList<EntityProcessor> _processors;

        /// <summary>
        /// Iniitalizes a new instance of <see cref="Xenon.Game.Entities.EntityProcessorGroup"/>
        /// </summary>
        /// <param name="name"></param>
        public EntityProcessorGroup(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new ArgumentNullException("name", "The name of this EntityProcessor must not be null");

            _name = name;
            _processors = new List<EntityProcessor>();
        }

        /// <summary>
        /// Adds an <see cref="Xenon.Game.Entities.EntityProcessor"/> to this group
        /// </summary>
        /// <param name="processor"></param>
        public void Add(EntityProcessor processor)
        {
            if (_processors.Contains(processor))
                throw new InvalidOperationException("");
            else
                _processors.Add(processor);
        }

        /// <summary>
        /// Removes an <see cref="Xenon.Game.Entities.EntityProcessor"/> from this group if found
        /// </summary>
        /// <param name="processor"></param>
        public void Remove(EntityProcessor processor)
        {
            if (Contains(processor))
                _processors.Remove(processor);
            else
                throw new InvalidOperationException("");
        }

        /// <summary>
        /// Determines if the specified <see cref="Xenon.Game.Entities.EntityProcessor"/> is contained within this group
        /// </summary>
        /// <param name="processor"></param>
        /// <returns></returns>
        public bool Contains(EntityProcessor processor)
        { return _processors.Contains(processor); }

        public override int GetHashCode()
        { return _name.GetHashCode(); }

        /// <summary>
        /// Gets the number of <see cref="Xenon.Game.Entities.EntityProcessor"/>s within this group
        /// </summary>
        public int Count
        { get { return _processors.Count; } }

        /// <summary>
        /// Gets the name of this group
        /// </summary>
        public string Name
        { get { return _name; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public EntityProcessor this[int index]
        { get { return _processors[index]; } }

        /// <summary>
        /// Returns a readonly collection of <see cref="Xenon.Game.Entities.EntityProcessor"/>s
        /// </summary>
        public ReadOnlyList<EntityProcessor> Processors
        { get { return new ReadOnlyList<EntityProcessor>(_processors); } }
    }
}
